/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    ListNode* dfs(ListNode* list1, ListNode* list2)
    {
        if (!list1)
            return list2;
        if (!list2)
            return list1;
        ListNode* res = nullptr;
        if (list1->val <= list2->val)
        {
            list1->next = dfs(list1->next, list2);
            res = list1;
        }
        else
        {
            list2->next = dfs(list1, list2->next);
            res = list2;
        }
        return res;
    }
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        return dfs(list1, list2);
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* newhead = nullptr;
    ListNode* reverse(ListNode* root)
    {
        if (!root)
            return nullptr;
        newhead = root;
        ListNode* res = reverse(root->next);
        if (res)
        {
            root->next = nullptr;
            res->next = root;
        }

        return root;
    }
    ListNode* reverseList(ListNode* head) {
        reverse(head);
        return newhead;
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* Swap(ListNode* head)
    {
        if (!head || !head->next)
            return head;
        ListNode* h1 = head;
        ListNode* h2 = head->next;
        h1->next = Swap(h2->next);
        h2->next = h1;
        return h2;
    }
    ListNode* swapPairs(ListNode* head) {
        return Swap(head);
    }
};


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool dfs(TreeNode* root)
    {
        if (root->val == 0 || root->val == 1)
            return root->val;
        if (root->val == 2)
            return dfs(root->left) | dfs(root->right);
        else return dfs(root->left) & dfs(root->right);
    }
    bool evaluateTree(TreeNode* root) {
        return dfs(root);
    }
};

